<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <!-- 基础题库 -->
        <div class="btn-wrapper">
          <span style="font-size: 12px; color: pink"
            >说明：目前支持学科和关键字条件筛选
          </span>
          <el-button type="success" icon="el-icon-edit">新增试题</el-button>
          <!-- <el-button type="success" class=""></el-button> -->
        </div>
        <el-form ref="form" :model="form" label-width="80px">
          <el-row>
            <!-- 学科 -->
            <el-col :span="6">
              <el-form-item label="学科" size="small">
                <el-select
                  v-model="form.subjectName"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in subjectlist"
                    :key="item.id"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <!-- 二级目录 -->
            <el-col :span="6">
              <el-form-item label="二级目录" size="small">
                <el-select
                  v-model="form.catalogID"
                  placeholder="请选择"
                  style="width: 100%"
                >
                </el-select>
              </el-form-item>
            </el-col>
            <!-- 标签 -->
            <el-col :span="6">
              <el-form-item label="标签" size="small">
                <el-select
                  v-model="form.tags"
                  placeholder="请选择"
                  style="width: 100%"
                >
                </el-select>
              </el-form-item>
            </el-col>
            <!-- 根据题干搜索 -->
            <el-col :span="6">
              <el-form-item label="关键字">
                <el-input
                  v-model="form.question"
                  style="width: 100%"
                  placeholder="根据题干搜索"
                ></el-input>
              </el-form-item>
            </el-col>
            <!-- 试题类型 -->
            <el-col :span="6">
              <el-form-item label="试题类型" size="small">
                <el-select
                  v-model="form.questionType"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option
                    v-for="(item, index) in questionType"
                    :key="index"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <!-- 难度 -->
            <el-col :span="6">
              <el-form-item label="难度" size="small">
                <el-select
                  v-model="form.difficulty"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option
                    v-for="(item, index) in difficulty"
                    :key="index"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <!-- 方向 -->
            <el-col :span="6">
              <el-form-item label="方向" size="small">
                <el-select
                  v-model="form.direction"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in direction"
                    :key="item"
                    :label="item"
                    :value="item"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <!-- 录入人 -->
            <el-col :span="6">
              <el-form-item label="录入人" size="small">
                <el-select
                  v-model="form.creator"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in authorList"
                    :key="item.id"
                    :label="item.username"
                    :value="item.username"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <!-- 题目备注 -->
            <el-col :span="6">
              <el-form-item label="题目备注">
                <el-input v-model="form.remarks" style="width: 100%"></el-input>
              </el-form-item>
            </el-col>
            <!-- 企业简称 -->
            <el-col :span="6">
              <el-form-item label="企业简称">
                <el-input
                  v-model="form.enterpriseID"
                  style="width: 100%"
                ></el-input>
              </el-form-item>
            </el-col>
            <!-- 城市 -->
            <el-col :span="6">
              <el-form-item label="城市" size="small">
                <!-- 省份 -->
                <el-select
                  v-model="form.province"
                  placeholder="请选择"
                  style="width: 50%"
                  @change="cityschange"
                >
                  <el-option
                    v-for="item in cityslist"
                    :key="item"
                    :label="item"
                    :value="item"
                  ></el-option>
                </el-select>
                <!-- 区县 -->
                <el-select
                  v-model="form.city"
                  placeholder="请选择"
                  style="width: 50%"
                >
                  <el-option
                    v-for="item in citys"
                    :key="item"
                    :label="item"
                    :value="item"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <!-- 清除/搜索按钮 -->
            <el-col :span="6">
              <el-form-item style="text-align: right">
                <el-button plain size="small" @click="delform">清除</el-button>
                <el-button type="primary" plain size="small">搜索</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
      <el-alert title="" type="info" show-icon>数据一共{{ total }}条</el-alert>
      <el-table :data="list" style="width: 100%">
        <el-table-column prop="number" label="试题编号" width="180">
        </el-table-column>
        <el-table-column prop="subject" label="学科" width="180">
        </el-table-column>
        <el-table-column prop="catalog" label="目录"> </el-table-column>
        <el-table-column prop="questionType" label="题型" width="180">
          <template #default="{ row }">
            {{ question[row.questionType] }}
          </template> </el-table-column
        ><el-table-column prop="question" label="题干" width="180">
          <template #default="{ row }">
            <p v-html="row.question">{{ row.question }}</p>
          </template> </el-table-column
        ><el-table-column prop="addDate" label="录入时间" width="180">
          <template #default="{ row }">
            {{ formatDate(row.addDate) }}
          </template> </el-table-column
        ><el-table-column prop="difficulty" label="难度" width="180">
          <template #default="{ row }">
            {{ diff[row.difficulty] }}
          </template> </el-table-column
        ><el-table-column prop="creator" label="录入人" width="180">
        </el-table-column
        ><el-table-column label="操作" width="180">
          <template #default="{ row }">
            <el-button
              type="primary"
              icon="el-icon-view"
              circle
              size="small"
              @click="showDialog(row.id)"
            ></el-button>
            <el-button
              type="success"
              icon="el-icon-edit"
              circle
              size="small"
              @click="updateQuestion(row.id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              size="small"
              @click="delQuestion(row.id)"
            ></el-button>
            <el-button
              type="warning"
              icon="el-icon-check"
              circle
              size="small"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" justify="end" align="middle" style="height: 60px">
        <!-- 分页器 -->
        <el-pagination
          style="text-align: right"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="params.page"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="10"
          layout="prev, pager, next, sizes,jumper"
          :total="total"
        >
        </el-pagination>
      </el-row>
    </div>
    <list-add
     :dialogVisible="dialogVisible"
     @close-dialog="closeDialog"
     :quesID="id"
     :question="question"
     :diff="diff"
    >
    </list-add>
  </div>
</template>
<script>
import { list, remove } from '@/api/hmmm/questions.js'
import { simple } from '@/api/hmmm/subjects.js'
import { difficulty, questionType, direction } from '@/api/hmmm/constants.js'
import { provinces, citys } from '@/api/hmmm/citys.js'
import { list as getUserList } from '@/api/base/users'
import dayjs from 'dayjs'
import ListAdd from '@/module-hmmm/components/list-add'

export default {
  components: {
    ListAdd
  },
  data () {
    return {
      // 控制预览弹窗的显示和隐藏
      dialogVisible: false,
      id: 0,
      value: '',
      form: {
        subjectName: '',
        catalogID: '',
        tags: '',
        question: '',
        questionType: '',
        difficulty: '',
        direction: '',
        creator: '',
        remarks: '',
        enterpriseID: '',
        province: '',
        city: ''
      },
      list: [],
      total: 0,
      // 分页器页码
      params: {
        page: 1,
        pagesize: 10
      },
      // 基础题库列表
      subjectlist: [],
      difficulty: difficulty,
      questionType: questionType,
      direction: direction,
      // 城市
      cityslist: [],
      // 区县
      citys: [],
      // 录入人数据
      authorList: [],
      // 试题类型枚举值
      question: {
        1: '单选',
        2: '多选',
        3: '简单'
      },
      // 难度枚举值
      diff: {
        1: '简单',
        2: '一般',
        3: '困难'
      }
    }
  },
  methods: {
    handleSizeChange (val) {
      this.params.pagesize = val
      this.questionList()
      // console.log(`每页 ${val} 条`)
    },
    handleCurrentChange (val) {
      this.params.page = val
      this.questionList()
      // console.log(`当前页: ${val}`)
    },
    // 获取基础题库列表
    async questionList () {
      const res = await list(this.params)
      // console.log(res)
      this.list = res.data.items
      this.total = res.data.counts
    },
    // 获取学科列表
    async getsubjectlist () {
      const res = await simple()
      // console.log(res)
      this.subjectlist = res.data
    },
    // 获取城市列表
    getcitylist () {
      this.cityslist = provinces()
    },
    // 获取区县列表
    cityschange () {
      this.form.city = ''
      this.citys = citys(this.form.province)
      // console.log(this.citys)
    },
    // 点击清除
    delform () {
      this.form = {
        subjectName: '',
        catalogID: '',
        tags: '',
        question: '',
        questionType: '',
        difficulty: '',
        direction: '',
        creator: '',
        remarks: '',
        enterpriseID: '',
        province: '',
        city: ''
      }
    },
    // 录入人
    async fetchUserList () {
      const res = await getUserList()
      // console.log('录入人数据：', res)
      this.authorList = res.data.list
    },
    // 格式化时间
    formatDate (value) {
      return dayjs(value).format('YYYY-MM-DD HH:mm:ss')
    },
    closeDialog () {
      this.dialogVisible = false
    },
    // 预览的弹窗
    showDialog (id) {
      this.dialogVisible = true
      this.id = id
    },
    // 修改
    updateQuestion (id) {
      this.$router.push({
        path: 'new',
        query: {
          id
        }
      })
    },
    delQuestion (id) {
      // 1. 询问用户
      this.$confirm('此操作将永久删除该问题, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 2. 调用接口
        await remove({
          id
        })
        // 3. 提示用户
        this.$message.success('删除成功')
        // 4. 刷新列表
        this.fetchQuestionList()
      })
    }
  },
  created () {
    this.questionList()
    this.getsubjectlist()
    this.getcitylist()
    this.cityschange()
    this.fetchUserList()
  }
}
</script>

<style scoped>
.btn-wrapper {
  margin-bottom: 15px;
  display: flex;
  justify-content: space-between;
}
</style>
